package com.matrix.media.utils;

public class FileUtil {

    public static String getFileExtension(String fileUrl) {
        if (fileUrl == null || fileUrl.isEmpty()) {
            return "";
        }
        int lastDotIndex = fileUrl.lastIndexOf('.');
        if (lastDotIndex > 0 && lastDotIndex < fileUrl.length() - 1) {
            return fileUrl.substring(lastDotIndex + 1);
        } else {
            return "";
        }
    }

    public static boolean isValidFile(String fileUrl) {
        if (isVideo(fileUrl) || isImage(fileUrl) || isAudio(fileUrl)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isVideo(String fileUrl) {
        String extension = getFileExtension(fileUrl);
        return extension.equalsIgnoreCase("mp4") || extension.equalsIgnoreCase("avi") || extension.equalsIgnoreCase("flv");
    }

    public static boolean isImage(String fileUrl) {
        String extension = getFileExtension(fileUrl);
        return extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("png");
    }

    public static boolean isAudio(String fileUrl) {
        String extension = getFileExtension(fileUrl);
        return extension.equalsIgnoreCase("mp3") || extension.equalsIgnoreCase("wav") || extension.equalsIgnoreCase("flac");
    }
}
